home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
sample.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
246 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* sample -
* Support for managing sample sets.
*
* Paul Haeberli - 1988
*/
#include "ss.h"
#include "vect.h"
#include "stdio.h"
#include "resource.h"
#include "chunkarena.h"
static chunkarena *samparena;
sample *samplemalloc()
{
if(!samparena)
samparena = newchunkalloc(sizeof(sample));
return (sample *)chunkalloc(samparena);
}
sfree(s)
sample *s;
{
chunkfree(samparena,s);
}
sample *snew(p,c,at0,at1,at2,at3)
vect *p, *c;
int at0, at1, at2, at3;
{
sample *s;
s = samplemalloc();
s->xpos = POSSCALE*p->x;
s->ypos = POSSCALE*p->y;
s->r = 255*c->x;
s->g = 255*c->y;
s->b = 255*c->z;
s->a = 255*c->w;
s->at0 = at0;
s->at1 = at1;
s->at2 = at2;
s->at3 = at3;
return s;
}
sample *sclone(s)
sample *s;
{
sample *ns;
ns = samplemalloc();
*ns = *s;
return ns;
}
sprint(s)
sample *s;
{
printf("sample: pos %f %f color %3d %3d %3d %3d attribs: %3d %3d %3d\n",
s->xpos/(float)POSSCALE,s->ypos/(float)POSSCALE,
s->r, s->g, s->b, s->a,
s->at0, s->at1, s->at2);
}
sampleset *ssnew()
{
sampleset *ss;
ss = (sampleset *)mymalloc(sizeof(sampleset));
ss->head = 0;
ss->tail = 0;
ss->nsamples = 0;
return ss;
}
ssfree(ss)
sampleset *ss;
{
ssclear(ss);
free(ss);
}
ssclear(ss)
sampleset *ss;
{
sample *s, *ns;
s = ss->head;
while(s) {
ns = s->next;
sfree(s);
s = ns;
}
ss->head = 0;
ss->tail = 0;
ss->nsamples = 0;
}
sampleset *ssclone(ss)
sampleset *ss;
{
sampleset *nss;
sample *s, *ns;
nss = ssnew();
s = ss->head;
while(s) {
ns = sclone(s);
addsample(nss,ns);
s = s->next;
}
return nss;
}
ssprint(ss)
sampleset *ss;
{
sample *s;
printf("sample set with %d samples siz dir brush\n",ss->nsamples);
s = ss->head;
while(s) {
sprint(s);
s = s->next;
}
}
addsample(ss,s)
sampleset *ss;
sample *s;
{
s->next = 0;
if(!ss->tail) {
ss->head = s;
ss->tail = s;
ss->nsamples = 1;
} else {
ss->tail->next = s;
ss->tail = s;
ss->nsamples++;
}
}
sslerp(ss0,ss1,ss,p)
sampleset *ss0, *ss1, *ss;
float p;
{
sample *s0, *s1, *s, *ns;
ssclear(ss);
s0 = ss0->head;
s1 = ss1->head;
if(!s0 || !s1)
return;
while(1) {
s = samplemalloc();
slerp(s0,s1,s,p);
addsample(ss,s);
if(s0->next)
s0 = s0->next;
if(s1->next)
s1 = s1->next;
if((s0->next==0) && (s1->next == 0))
return;
}
}
slerp(s0,s1,s,p)
sample *s0, *s1, *s;
float p;
{
s->xpos = lerp(s0->xpos,s1->xpos,p);
s->ypos = lerp(s0->ypos,s1->ypos,p);
s->r = lerp(s0->r,s1->r,p);
s->g = lerp(s0->g,s1->g,p);
s->b = lerp(s0->b,s1->b,p);
s->a = lerp(s0->a,s1->a,p);
s->at0 = lerp(s0->at0,s1->at0,p);
s->at1 = lerp(s0->at1,s1->at1,p);
s->at2 = lerp(s0->at2,s1->at2,p);
s->at3 = lerp(s0->at3,s1->at3,p);
}
sstofile(name,ss)
char *name;
sampleset *ss;
{
FILE *outf;
sample *s;
outf = fopen(name,"w");
if(!outf) {
fprintf(stderr,"sstofile: can't open output file %s\n",name);
exit(1);
}
fwrite(&ss->nsamples,1,sizeof(int),outf);
s = ss->head;
while(s) {
fwrite(s,1,sizeof(sample),outf);
s = s->next;
}
fclose(outf);
}
sampleset *ssfromfile(name)
char *name;
{
int i, nsamples;
sampleset *ss;
sample *s;
FILE *inf;
inf = res_fopen(name,"r");
if(!inf) {
fprintf(stderr,"sstofile: can't open input file %s\n",name);
exit(1);
}
ss = ssnew();
res_fread(&nsamples,1,sizeof(int),inf);
for(i=0; i<nsamples; i++) {
s = samplemalloc();
res_fread(s,1,sizeof(sample),inf);
addsample(ss,s);
}
res_fclose(inf);
return ss;
}